package com.jrfid.smartcabinet.demo.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.arcsoft.face.FaceInfo
import com.arcsoft.face.LivenessInfo
import com.blankj.utilcode.util.SizeUtils
import com.jrfid.smartcabinet.demo.model.DrawFaceInfoModel

class FaceRectView : View {
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(context, attrs, defStyleAttr, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        initView()
    }

    private lateinit var rectPaint: Paint
    private lateinit var textPaint: Paint

    private fun initView() {
        rectPaint = Paint()
            .apply {
                this.style = Paint.Style.STROKE
                this.strokeWidth = SizeUtils.dp2px(2f).toFloat()
                this.color = Color.GREEN
                this.isAntiAlias = true
            }
        textPaint = Paint().apply {
            this.style = Paint.Style.FILL_AND_STROKE
            this.textSize = SizeUtils.sp2px(22f).toFloat()
            this.strokeWidth = 1f
            this.color = Color.GREEN
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        faceInfo?.let {
            it.faceRect?.let { rect ->
                var text = ""
                if (it.isRegistered) {
                    //已注册
                    text = "Registered"
                    textPaint.color = Color.GREEN
                    rectPaint.color = Color.GREEN
                } else {
                    //非真人
                    text = "Unregistered"
                    textPaint.color = Color.RED
                    rectPaint.color = Color.RED
                }
                if (text.isNotEmpty()) {
                    canvas.drawText(text, rect.left.toFloat(), (rect.top - 10).toFloat(), textPaint)
                }
                canvas.drawRect(rect, rectPaint)
            }
        }
    }

    /**
     * 人脸数据
     */
    private var faceInfo: DrawFaceInfoModel? = null


    fun setDrawFaceInfo(faceInfo: DrawFaceInfoModel?) {
        this.faceInfo = faceInfo
        postInvalidate()
    }

    fun clearFaceInfo() {
        this.faceInfo = null
        postInvalidate()
    }

}